package simulator.drawengine;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

import simulator.drawengine.Drawable;

public class DrawEngine extends JComponent
{
	private List<Drawable>	_updateList;
	
	private BufferedImage bgImage;

	public DrawEngine()
	{
		_updateList = new ArrayList<>();
		
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		InputStream input = classLoader.getResourceAsStream("Background.png");
		try
		{
			bgImage = ImageIO.read(input);
		} catch (IOException e)
		{
			e.printStackTrace();
			System.exit(0);
		}
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		g.drawImage(bgImage, 0, 0, null);
		
		for(int i=0; i<_updateList.size(); i++)
		{
			_updateList.get(i).draw(g);
		}
	}
	
	public void add(Drawable u)
	{
		_updateList.add(u);
	}
	
	public void remove(Drawable o)
	{
		_updateList.remove(o);
	}
	
	public final static float GFX_ZOOM = 10; //pixels per meter for rendering
	public final static float GFX_CENTER_X = 400; //center pixel X-coord of rendering
	public final static float GFX_CENTER_Y = 400; //center pixel Y-coord of rendering
	
	/**
	 * Change a simulation x-position to a graphics x-position
	 * @param x
	 * x-position in simulation of thing to draw
	 * @return
	 * x-position for graphics of thing to draw
	 */
	public static int getGraphicsX(float x)
	{
		return (int)((x * GFX_ZOOM) + GFX_CENTER_X);
	}
	
	/**
	 * Change a simulation y-position to a graphics y-position
	 * @param y
	 * y-position in simulation of thing to draw
	 * @return
	 * y-position for graphics of thing to draw
	 */
	public static int getGraphicsY(float y)
	{
		return (int)((-y * GFX_ZOOM) + GFX_CENTER_Y);
	}
}
